home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / java / vectors / frame1.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-02-06  |  5.8 KB  |  185 lines

  1. import java.awt.Component;
  2. import java.awt.Container;
  3. import java.awt.Event;
  4. import java.awt.FileDialog;
  5. import java.awt.Frame;
  6. import java.awt.Label;
  7. import java.awt.LayoutManager;
  8. import java.awt.List;
  9. import java.awt.Menu;
  10. import java.awt.MenuBar;
  11. import java.awt.MenuItem;
  12. import java.util.Enumeration;
  13. import java.util.Vector;
  14.  
  15. public class Frame1 extends Frame {
  16.    Vector thingsInStock;
  17.    Vector thingsInBasket;
  18.    FileDialog OpenFileDialog;
  19.    List StockList;
  20.    List BasketList;
  21.    Label label1;
  22.    Label label2;
  23.    Label label3;
  24.    Label label4;
  25.    MenuBar mainMenuBar;
  26.    Menu menu1;
  27.    Menu menu2;
  28.    Menu menu3;
  29.  
  30.    void MoveObFromV1ToV2(Object Ob, Vector V1, Vector V2) {
  31.       V2.addElement(Ob);
  32.       V1.removeElement(Ob);
  33.    }
  34.  
  35.    void BasketList_DblClick(Event event) {
  36.       Object thing = this.thingsInBasket.elementAt(this.BasketList.getSelectedIndex());
  37.       this.MoveObFromV1ToV2(thing, this.thingsInBasket, this.thingsInStock);
  38.       this.UpdateListBoxes();
  39.    }
  40.  
  41.    void StockList_DblClick(Event event) {
  42.       Object thing = this.thingsInStock.elementAt(this.StockList.getSelectedIndex());
  43.       this.MoveObFromV1ToV2(thing, this.thingsInStock, this.thingsInBasket);
  44.       this.UpdateListBoxes();
  45.    }
  46.  
  47.    void UpdateListBox(List l, Vector v) {
  48.       l.clear();
  49.       Enumeration e = v.elements();
  50.  
  51.       while(e.hasMoreElements()) {
  52.          l.addItem(e.nextElement().toString());
  53.       }
  54.  
  55.    }
  56.  
  57.    void UpdateListBoxes() {
  58.       this.UpdateListBox(this.BasketList, this.thingsInBasket);
  59.       this.UpdateListBox(this.StockList, this.thingsInStock);
  60.    }
  61.  
  62.    void Open_Action(Event event) {
  63.       this.OpenFileDialog.show();
  64.    }
  65.  
  66.    void About_Action(Event event) {
  67.       (new AboutDialog(this, "About...", false)).show();
  68.    }
  69.  
  70.    void Exit_Action(Event event) {
  71.       (new QuitDialog(this, "Quit the Application?", false)).show();
  72.    }
  73.  
  74.    public Frame1() {
  75.       ((Container)this).setLayout((LayoutManager)null);
  76.       ((Frame)this).addNotify();
  77.       ((Component)this).resize(((Container)this).insets().left + ((Container)this).insets().right + 457, ((Container)this).insets().top + ((Container)this).insets().bottom + 322);
  78.       this.OpenFileDialog = new FileDialog(this, "Open", 0);
  79.       this.StockList = new List(0, false);
  80.       ((Container)this).add(this.StockList);
  81.       this.StockList.reshape(((Container)this).insets().left + 14, ((Container)this).insets().top + 45, 201, 225);
  82.       this.BasketList = new List(0, false);
  83.       ((Container)this).add(this.BasketList);
  84.       this.BasketList.reshape(((Container)this).insets().left + 238, ((Container)this).insets().top + 45, 201, 225);
  85.       this.label1 = new Label("ITEMS IN STOCK");
  86.       this.label1.reshape(((Container)this).insets().left + 14, ((Container)this).insets().top + 15, 196, 15);
  87.       ((Container)this).add(this.label1);
  88.       this.label2 = new Label("ITEMS IN YOUR BASKET");
  89.       this.label2.reshape(((Container)this).insets().left + 238, ((Container)this).insets().top + 15, 203, 15);
  90.       ((Container)this).add(this.label2);
  91.       this.label3 = new Label("DOUBLE-CLICK TO BUY");
  92.       this.label3.reshape(((Container)this).insets().left + 14, ((Container)this).insets().top + 278, 203, 22);
  93.       ((Container)this).add(this.label3);
  94.       this.label4 = new Label("DOUBLE-CLICK TO PUT BACK");
  95.       this.label4.reshape(((Container)this).insets().left + 238, ((Container)this).insets().top + 278, 203, 24);
  96.       ((Container)this).add(this.label4);
  97.       ((Frame)this).setTitle("A Basic Application");
  98.       this.mainMenuBar = new MenuBar();
  99.       this.menu1 = new Menu("File");
  100.       this.menu1.add("Open...");
  101.       this.menu1.add("Save");
  102.       this.menu1.add("Save As...");
  103.       this.menu1.addSeparator();
  104.       this.menu1.add("Exit");
  105.       this.mainMenuBar.add(this.menu1);
  106.       this.menu2 = new Menu("Edit");
  107.       this.menu2.add("Cut");
  108.       this.menu2.add("Copy");
  109.       this.menu2.add("Paste");
  110.       this.mainMenuBar.add(this.menu2);
  111.       this.menu3 = new Menu("Help");
  112.       this.menu3.add("About");
  113.       this.mainMenuBar.add(this.menu3);
  114.       ((Frame)this).setMenuBar(this.mainMenuBar);
  115.    }
  116.  
  117.    public Frame1(String title) {
  118.       this();
  119.       ((Frame)this).setTitle(title);
  120.    }
  121.  
  122.    public synchronized void show() {
  123.       ((Component)this).move(50, 50);
  124.       super.show();
  125.       this.thingsInStock = new Vector();
  126.       this.thingsInBasket = new Vector();
  127.       this.thingsInStock.addElement("Tin of Schpamm");
  128.       this.thingsInStock.addElement("Pot of Schnoodles");
  129.       this.thingsInStock.addElement("Jug of Hare");
  130.       this.thingsInStock.addElement("Kettle of Fish");
  131.       this.thingsInStock.addElement("Bombay Duck");
  132.       this.thingsInStock.addElement("Duck-billed platypus");
  133.       this.thingsInStock.addElement("Can of worms");
  134.       this.thingsInStock.addElement("Hair of the dog");
  135.       this.thingsInStock.addElement("Dog's bone");
  136.       this.thingsInStock.addElement("PC Plus");
  137.       this.UpdateListBoxes();
  138.    }
  139.  
  140.    public boolean handleEvent(Event event) {
  141.       if (event.id == 201) {
  142.          ((Component)this).hide();
  143.          ((Frame)this).dispose();
  144.          System.exit(0);
  145.          return true;
  146.       } else {
  147.          if (event.target == this.StockList && event.id == 1001) {
  148.             this.StockList_DblClick(event);
  149.          }
  150.  
  151.          if (event.target == this.BasketList && event.id == 1001) {
  152.             this.BasketList_DblClick(event);
  153.          }
  154.  
  155.          return super.handleEvent(event);
  156.       }
  157.    }
  158.  
  159.    public boolean action(Event event, Object arg) {
  160.       if (event.target instanceof MenuItem) {
  161.          String label = (String)arg;
  162.          if (label.equalsIgnoreCase("Open...")) {
  163.             this.Open_Action(event);
  164.             return true;
  165.          }
  166.  
  167.          if (label.equalsIgnoreCase("About")) {
  168.             this.About_Action(event);
  169.             return true;
  170.          }
  171.  
  172.          if (label.equalsIgnoreCase("Exit")) {
  173.             this.Exit_Action(event);
  174.             return true;
  175.          }
  176.       }
  177.  
  178.       return super.action(event, arg);
  179.    }
  180.  
  181.    public static void main(String[] args) {
  182.       (new Frame1()).show();
  183.    }
  184. }
  185.